package com.passion.controller;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.passion.bean.OfflineAnalyseResult;
import com.passion.common.R;
import com.passion.service.OfflineAnalyseResultService;
import com.passion.service.OfflineAnalyseResultTableService;
import com.passion.utils.DateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @program offline-e-commerce-data-warehouse
 * @Description
 * @Author limeihua
 * @Date 2024/10/02 23:33
 * @Version 1.0
 */
@RestController
@RequestMapping("/sector")
public class SectorAnalyseController {
    @Autowired
    private OfflineAnalyseResultService offlineAnalyseResultService;

    @PostMapping("/universalAnalyse")
    public R test(@RequestBody Map<String, Object> req) {
        String date = (String) req.get("date");
        if (req.get("date") == null) {
            // 获取昨天的日期
            date = DateTimeUtil.timeStampToDate(System.currentTimeMillis() - 86400 * 1000);
        }
        OfflineAnalyseResult result = offlineAnalyseResultService.getOne(new QueryWrapper<OfflineAnalyseResult>().eq("sql_id", req.get("sqlID")).eq("analyse_time", date));
        if (result == null) {
            return R.ok();
        }
        String analyseResult = result.getAnalyseResult();
        String name = (String) req.get("name");
        String value = (String) req.get("value");
        String replace = analyseResult.replace("\"" + name + "\"", "\"name\"").replace("\"" + value + "\"", "\"value\"");
        JSONArray jsonArray = JSONArray.parseArray(replace);
        return R.ok(jsonArray);
    }
}
